﻿using FySystem.Logs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace FyAutoStartService
{
    internal class MessageListener
    {
        /// <summary>
        /// 初始化消息服务并进行监听
        /// </summary>
        internal void InitMessageListener()
        {
            try
            {
                string url = "ws://0.0.0.0:20011";
                if (Global.Ipv == "6")
                    url = $"ws://[{IPAddress.IPv6Any}]:20011";

                WSocketListenerHelper listener = new WSocketListenerHelper(url);
                Global.Listener = listener;
                listener.OnReceiveCallback = msg =>
                {
                    //收到消息回调
                    FyLogContextV2.WriteLog($"rev,websocket:{msg}");
                };
                listener.OnSendCallback = msg =>
                {
                    //发送消息回调
                    FyLogContextV2.WriteLog($"send,websocket:{msg}");
                };
                listener.OnWebSubscribeTopic = (msginfo, socket) =>
                {
                    //前端订阅主题回调

                };
                listener.OnCloseCallback = socket =>
                {
                    //websocket关闭回调
                    FyLogContextV2.WriteLog($"连接关闭:{socket.ConnectionInfo.ClientIpAddress}");
                };
                listener.OnException = (socket, error) =>
                {
                    FyLogContextV2.WriteError(error.Message);
                };
                listener.Start();

                //后端订阅主题，相当于是api接口，区分大小写
                listener.SubscribeTopic("/Api/Login", WSocketApi.Login);                                        //登录
                listener.SubscribeTopic("/Api/GetAllApps", WSocketApi.GetAllApi);                               //获取所有的自启app
                listener.SubscribeTopic("/Api/AddApp", WSocketApi.AddApp);                                      //添加或修改自启程序
                listener.SubscribeTopic("/Api/StartApp", WSocketApi.StartApp);                                  //开启程序
                listener.SubscribeTopic("/Api/StopApp", WSocketApi.StopApp);                                    //关闭程序
                listener.SubscribeTopic("/Api/DeleteApp", WSocketApi.DeleteApp);                                //删除程序

                listener.SubscribeTopic("/Api/ListDir", WSocketFileApi.ListDir);                                //列出目录
                listener.SubscribeTopic("/Api/GetDownloadFiles", WSocketFileApi.GetDownloadFiles);              //获取需要下载的文件列表
                listener.SubscribeTopic("/Api/CreateUploadTask", WSocketFileApi.CreateUploadTask);              //客户端请求上传文件

                FyLogContextV2.WriteLog($"服务启动，监听地址:{url}");
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 初始化文件传输服务
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        internal static FileTransfer InitFileTransfer()
        {
            try
            {
                string url = "ws://0.0.0.0:20012";
                if (Global.Ipv == "6")
                    url = $"ws://[{IPAddress.IPv6Any}]:20012";

                FileTransfer transfer = new FileTransfer(url);
                Global.FileTransfer = transfer;

                transfer.OnException = (wsock, ex) =>
                {
                    FyLogContextV2.WriteError(ex.Message);
                };
                transfer.Start();

                FyLogContextV2.WriteLog($"文件传输服务启动，监听地址:{url}");

                return transfer;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
